<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/1/9
 * Time: 下午3:39
 */

namespace Biz\model\trade;

use Biz\pockets\constInterface\PayConfig;
use Doraemon\tools\Log;

class IapReceipt extends \Doraemon\pockets\datebase\BaseDbModel implements PayConfig
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_trade';

    const _DATABASE_ = 'shop_trade';

    const _TABLE_NAME = 'iap_receipt';

    public static function addIapReceiptData($uid, $receiptData)
    {
        $md5 = md5($receiptData);
        $recordUid = self::get('uid', [
            'md5' => $md5
        ]);
        if ($recordUid != $uid) {
            if ($recordUid) {
                Log::error('不同用户提交相同苹果支付信息',
                    [
                        'uid'          => $uid,
                        'receipt_data' => $receiptData
                    ]);
            }
            self::insert([
                'uid'          => $uid,
                'md5'          => $md5,
                'receipt_data' => $receiptData,
                'addtime'      => date('Y-m-d H:i:s')
            ]);
        }
    }


    public static function addCreateOrderInfo($payWay, $orderNo, $payAmount, $payOrderInfo, $ordreType = self::_PAY_ORDER_TYPE_BIZ, $payVer = 0)
    {
        $lastPayInfo = self::get("*", [
            'order_no'   => $orderNo,
            'pay_way'    => $payWay,
            'order_type' => $ordreType,
            'pay_ver'    => $payVer
        ]);
        if ($lastPayInfo) {
            self::update([
                'pay_order_info'    => json_encode($payOrderInfo),
                'create_order_time' => date('Y-m-d H:i:s')
            ], [
                'order_no'   => $orderNo,
                'pay_way'    => $payWay,
                'order_type' => $ordreType
            ]);
        } else {
            self::insert([
                'order_no'          => $orderNo,
                'pay_way'           => $payWay,
                'pay_amount'        => $payAmount,
                'pay_order_info'    => json_encode($payOrderInfo),
                'create_order_time' => date('Y-m-d H:i:s'),
                'order_type'        => $ordreType,
                'pay_ver'           => $payVer
            ]);
        }
    }
}